package com.wxq.day0415.config;

import com.wxq.day0415.formater.DateFormater;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.annotation.PostConstruct;

/**
 * @Author wxq
 * @Date 2021/4/15
 **/
@Configuration
@EnableWebMvc
@ComponentScan("com.wxq.day0415")
public class WebmvcConfig implements WebMvcConfigurer {


    @Autowired
    private DateFormater dateFormater;

    @Autowired
    private FormattingConversionService formattingConversionService;


    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    /**
     * 注册格式化转换器
     */
    /**
     * @PostConstruct 修饰的方法 会在服务器加载Servlet的时候运行，并且只会被服务器调用一次
     * 类似于Servlet中的init方法
     * @PostConstruct 修饰的方法会在构造函数之后，init方法之前运行
     */
    @PostConstruct
    public void initEditableAvlidation() {
        if (formattingConversionService != null) {
            formattingConversionService.addFormatter(dateFormater);
        }
    }
}
